package com.cdqidi.smsservice.util;

import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
@Component(value="textUnit")
public class TextUnit {

    public List<byte[]> getLongByte(String message) {
        List<byte[]> list = new ArrayList<>();
            try {
                byte[] msgContent;
                byte[] messageUCS2 = message.getBytes("UnicodeBigUnmarked");
                int messageUCS2Len = messageUCS2.length;// 长短信长度
                int maxMessageLen = 118;
                if (messageUCS2Len > maxMessageLen) {// 长短信发送
                    int messageUCS2Count = messageUCS2Len / (maxMessageLen) + 1;// 长短信分为多少条发送
                    for (int i = 0; i < messageUCS2Count; i++) {
                        if (i != messageUCS2Count - 1) {// 不为最后一条
                            msgContent = byteAdd(messageUCS2, i * (maxMessageLen), (i + 1) * (maxMessageLen));
                            list.add(msgContent);
                        } else {
                            msgContent = byteAdd(messageUCS2, i * (maxMessageLen), messageUCS2Len);
                            list.add(msgContent);
                        }
                    }
                } else {
                    msgContent = byteAdd(messageUCS2, 0, messageUCS2Len);
                    list.add(msgContent);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        return list;
    }

    public String[] shortMessageSplit(String content,Integer signLength) {
        //进行空值处理
        if (null == content || content.equals("") || content.length() < 1)
            return null;
        String[] list = null;
        try {
            int start;
            int end;
            byte[] msgContent;
            byte[] messageUCS2 = content.getBytes("UnicodeBigUnmarked");
            int messageUCS2Len = messageUCS2.length;// 长短信长度
            int maxMessageLen=108;
            if(null!=signLength&&signLength>0){
                maxMessageLen = 118-signLength;
            }
            int messageUCS2Count = messageUCS2Len % maxMessageLen == 0 ? messageUCS2Len / maxMessageLen : messageUCS2Len / maxMessageLen + 1;
            list = new String[messageUCS2Count];
            for (int i = 0; i < messageUCS2Count; i++) {
                start = i * (maxMessageLen);
                end = (i + 1) * (maxMessageLen);
                if (i == messageUCS2Count - 1) {// 不为最后一条
                    end = messageUCS2Len;
                }
                msgContent = byteAdd(messageUCS2, start, end);
                String s = new String(msgContent, StandardCharsets.UTF_16);
                if (messageUCS2Count > 1) {
                    s = getSecondEnd(i, messageUCS2Count) + s;
                }
                list[i] = s;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public String crateSign(String body,String sign){
        StringBuilder content = new StringBuilder(body);
        int index = body.lastIndexOf("【");
        if(index==-1&&(null!=sign&&!"".equals(sign))){
            content.append("【");
            content.append(sign);
            content.append("】");
        }
        return content.toString();
    }


    private byte[] byteAdd(byte[] messageUCS2, int i, int j) {
        byte[] msgByte = new byte[j - i];
        System.arraycopy(messageUCS2, i, msgByte, 0, j - i);
        return msgByte;
    }

    private String getSecondEnd(int n, int spNum) {
        return "(" + (n + 1) + "/" + spNum + ")";
    }
}
